/*
 *
 *    Copyright (c) 2021 Project CHIP Authors
 *    All rights reserved.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

#include <AppMain.h>
#include <app/util/util.h>
#include "CHIPProjectAppConfig.h"

#include "src/MatterBridge.h"

using namespace chip;
using namespace chip::Credentials;
using namespace chip::DeviceLayer;

void ApplicationInit() {
    ChipLogProgress(DeviceLayer, "Ok,let's go!");
    ChipLogProgress(DeviceLayer, "DYNAMIC_ENDPOINT_COUNT: %d", CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT);

    if (BridgedMgr().LoadConfig("/userdata/matter/files/bridged_remote_config.json") != CHIP_NO_ERROR) {
        exit(-1);
    }

    // Set starting endpoint id where dynamic endpoints will be assigned, which
    // will be the next consecutive endpoint id after the last fixed endpoint.
    auto firstDynamicEndpoint = static_cast<EndpointId>(
            static_cast<int>(emberAfEndpointFromIndex(static_cast<uint16_t>(emberAfFixedEndpointCount() - 1))) + 1);
    ChipLogDetail(DeviceLayer, "firstDynamicEndpoint=%d", firstDynamicEndpoint);

    BridgedMgr().AttachNodes();
}

void ApplicationShutdown() {}

int main(int argc, char *argv[]) {
    ChipLogProgress(DeviceLayer, "Ok,let's go!");
    ChipLogProgress(DeviceLayer, "DYNAMIC_ENDPOINT_COUNT: %d", CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT);

    if (ChipLinuxAppInit(argc, argv) != 0) {
        return -1;
    }
    ChipLinuxAppMainLoop();
    return 0;
}

